//==============================================================================
//
//             Copyright (C) 2002-2005  Dick van Oudheusden
//  
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public
// License as published by the Free Software Foundation; either
// version 2 of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// General Public License for more details.
//
// You should have received a copy of the GNU General Public
// License along with this program; if not, write to the Free
// Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
//
//==============================================================================
// 
//  $Date: 2005-10-21 18:45:20 $ $Revision: 1.4 $
//
//==============================================================================

#include <stdlib.h>

#include "ofc/config.h"

#include "ofc/DData.h"

#include "DInc.h"
#include "DTest.h"

//-DataTypes-------------------------------------------------------------------

void DData_test()
{
  DData *string = [DData alloc];
  DData *copy;
  DText *text;
  DText *sub;
  DData *test = [DData new];
  
  unsigned char   ch;
  unsigned char   dt[] = { 0x20,0x67,0x12,0x80 };  
  unsigned char   bd[] = { 0x92, 0xfa, 0x56, 0x45, 0x10, 0xaa, 0x2c, 0xd5, 0x00, 0x1f, 0x6a };
  
  STARTTEST();

  [string init];
  [string set :"Hello world !!" :11];
  
  TEST([string bcompare :"hello world"  :11] != 0);
  TEST([string bcompare :"Hello world"  :11] == 0);
  TEST([string bcompare :"Hello world " :12] <  0);

  ch = [string pop];

  TEST((ch == 'd') && ([string bcompare :"Hello world" :10] == 0) && ([string length] == 10));

  [string set :"Daag Hello Week" :5 :9];

  TEST([string bcompare :"Hello" :5] == 0);

  [string delete :4];
  [string put    :3 :'p'];
  ch = [string get :0];

  TEST((ch == 'H') && ([string bcompare :"Help" :4] == 0));

  copy = [string copy];
  
  // Copy test
  TEST(string != copy);
  TEST([string data] != [copy data]);
  TEST([string compare :copy] == 0);

  [copy free];

  // Modifiers test
  TEST([[string append :" DAAG" :5] bcompare :"Help DAAG" :9] == 0); 

  [string free];

  // Range tests  
  string = [DData alloc];
  
  [string init];
  [string set :"This is a long text text" :0 :18];

  TEST([string bcompare :"This is a long text" :19] == 0);

  TEST([[string insert :15 :18 :"sentence" :8] bcompare :"This is a long sentence" :23] == 0); // remove and insert
  TEST([[string insert :0 :3 :"1." :2] bcompare :"1. is a long sentence" :21] == 0);  // remove and insert
  TEST([[string insert :8 :7 :"very " :5] bcompare :"1. is a very long sentence" :26] == 0); // true insert

  TEST([[string get :1 :-2] bcompare :". is a very long sentenc" :25] == 0); 

  TEST([[string delete :0 :1] bcompare :" is a very long sentence" :24] == 0);
  TEST([[string delete :16 :23] bcompare :" is a very long " :16] == 0);

  [string set :dt :sizeof(dt)];

  // base 64 tests
 
  [string set :bd :sizeof(bd)/sizeof(bd[0])];
 
  
  text = [string toBase64];
  
  TEST([text ccompare :"kvpWRRCqLNUAH2o="] == 0);

  [test fromBase64 :[text cstring]];
  
  TEST([test compare :string] == 0);
  
  [text free];
  
  
  text = [[string push :0x12] toBase64];
  
  TEST([text ccompare :"kvpWRRCqLNUAH2oS"] == 0);
  
  [test fromBase64 :[text cstring]];
  
  TEST([test compare :string] == 0);
  
  [text free];

    
  text = [[string push :0xfe] toBase64];
  
  TEST([text ccompare :"kvpWRRCqLNUAH2oS/g=="] == 0);

  [test fromBase64 :[text cstring]];
  
  TEST([test compare :string] == 0);
  
  [text free];

  
  text = [[string push :0xa9] toBase64];
  
  TEST([text ccompare :"kvpWRRCqLNUAH2oS/qk="] == 0);

  [test fromBase64 :[text cstring]];
  
  TEST([test compare :string] == 0);

  // Text scanner
  [string set :"This is a test of the scanText method" :38];
  sub = [string scanText :" atm" :&ch]; 
  TEST(sub != NULL);
  TEST([sub ccompare :"This"] == 0); 
  TEST(ch == ' ');
  [sub free];
  
  [string set :"This is a test of the scanText method" :38];
  sub = [string scanText :"mta" :&ch];
  TEST(sub != NULL);
  TEST([sub ccompare :"This is "] == 0);
  TEST(ch == 'a');
  [sub free];
  
  [string set :"This is a test of the scanText method" :38];
  sub = [string scanText :"bg" :&ch];
  TEST(sub == NULL);
  
  [text free];
  [test free];  
  [string free];
    
  STOPTEST();
}

